package com.neusoft.neuiotms.dcom.service.change.impl;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.converter.change.ChangeClosingConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeClosingDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeClosing;
import com.neusoft.neuiotms.dcom.repository.change.ChangeClosingRepository;
import com.neusoft.neuiotms.dcom.service.change.ChangeClosingService;

@Service
public class ChangeClosingServiceImpl implements ChangeClosingService {

    @Autowired
    private ChangeClosingRepository changeClosingRepository;

    @Autowired
    private ChangeClosingConverter changeClosingConverter;

    @Autowired
    private ProcessInstanceService processInstanceService;

    //    @Autowired
    //    private ChangePlanService changePlanService;
    //
    //    @Autowired
    //    private WorkOrderService workOrderService;
    //
    //    @Autowired
    //    private KnowledgeService knowledgeService;

    @Override
    @Transactional
    public ChangeClosingDTO create(final ChangeClosingDTO dto, final UserBean user) {
        // 获取处理信息
        //        final ChangePlanDTO changeDTO = this.changePlanService.findByWorkNum(dto.getWorkOrderNum());
        //        if (changeDTO != null) {
        //            // 存入知识库
        //            final KnowledgeDTO knowledgeDTO = new KnowledgeDTO();
        //            final WorkorderDTO workorderDTO = this.workOrderService.details(dto.getWorkOrderNum());
        //            knowledgeDTO.setTitle(workorderDTO.getTitle());
        //
        //            knowledgeDTO.setContent(
        //                    " <p> 可能存在的风险 -> " + changeDTO.getRisk() + " </p> " +
        //                            " <p> 风险处置措施 -> " + changeDTO.getRiskManagement() + " </p> " +
        //                            " <p> 实施方案 -> " + changeDTO.getImplementPlan() + " </p> " +
        //                            " <p> 回退方案 -> " + changeDTO.getFallbackPlan() + " </p> ");
        //            knowledgeDTO.setKnowledgeTypeCode("incident");
        //            this.knowledgeService.create(knowledgeDTO, user);
        //        }

        // 查询当前workNum和TaskId下是否存在记录
        final ChangeClosing oldModel = this.changeClosingRepository.findByWorkOrderNumAndTaskId(
                dto.getWorkOrderNum(), dto.getTaskId());
        if (oldModel == null) { // 如果不存在新增
            dto.setId(null);
        }

        final ChangeClosing model = this.changeClosingConverter.toModel(dto);
        this.changeClosingRepository.save(model);

        // 完成节点
        this.processInstanceService.complete(dto.getWorkOrderNum(), dto.getTaskId(), user);

        return this.changeClosingConverter.toDTO(model);
    }

    @Override
    public ChangeClosingDTO detail(final String workOrderNum, final String taskId, final UserBean user) {
        ChangeClosing model = null;
        try {
            if (StringUtils.isNotBlank(workOrderNum) && !"NULL".equals(taskId)) {
                model = this.changeClosingRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String fromProcessTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (fromProcessTaskId == null) {
                        return new ChangeClosingDTO();
                    } else {
                        model = this.changeClosingRepository.findByWorkOrderNumAndTaskId(workOrderNum,
                                fromProcessTaskId);
                    }
                }
            } else if ("NULL".equals(taskId)) {
                model = this.changeClosingRepository.findByWorkOrderNum(workOrderNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workOrderNum },
                    new Object[] {taskId });
        }
        return this.changeClosingConverter.toDTO(model);
    }

    @Override
    public ChangeClosingDTO findByWorkOrderNumAndTaskId(final String workOrderNum, final String taskId) {
        final ChangeClosing model = this.changeClosingRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
        return this.changeClosingConverter.toDTO(model);
    }

    @Override
    public void deleteByWorkOrderNum(final String workOrderNum) {
        this.changeClosingRepository.deleteByWorkOrderNum(workOrderNum);
    }

}
